$(function(){
	init();
	$("#btnAdd").click(function(){
		addInit();
		_addOrupdate = true;
		$('#window').modal('show');
	});
	$("#btnUpdate").click(function(){
		updateInit();
		_addOrupdate = false;
		$('#window').modal('show');
	});
	$("#btnDel").click(function(){
		$.post("/library-mannager/book/manage/delete.action",{"bookBean.bookId":$("#tab").datagrid("getSelected").bookId});
		$("#tab").datagrid({url:"/library-mannager/book/manage/init.action"});
	});
	$("#btnCancel").click(function(){
		$('#window').modal('hide');
	});
	$("#btnSave").click(function(){
//		$(".book").each(function(index){
//			if($(this).val() == "" || $(".book:eq(4)").datebox('getValue')==""){
//				//如果存在输入框未输入
//				$("#message").html("书籍信息填写不完整");
//				return false;
//			}
//		});
		var params ={
				"bookBean.bookId":$(".book:eq(0)").val(),
				"bookBean.bookName":$(".book:eq(1)").val(),
				"bookBean.bookAuthor":$(".book:eq(2)").val(),
				"bookBean.bookPublish":$(".book:eq(3)").val(),
				"bookBean.bookDate":$(".book:eq(4)").datebox('getValue'),
				"bookBean.bookType":$(".book:eq(5)").val(),
				"bookBean.bookNumber":$(".book:eq(6)").val(),
				"bookBean.bookSummary":$(".book:eq(7)").val()
		};
		if(_addOrupdate){
			$.post("/library-mannager/book/manage/add.action",params,function(data){
				$('#window').modal('hide');
				$.messager.show({  
	                title:'message',  
	                msg:data.message.msg,  
	                showType:'show'  
	            });
				$("#tab").datagrid({url:"/library-mannager/book/manage/init.action"});
			});
		}else{
			$.post("/library-mannager/book/manage/update.action",params,function(data){
				$('#window').modal('hide');
				$.messager.show({  
	                title:'message',  
	                msg:data.message.msg,  
	                showType:'show'  
	            });
				$("#tab").datagrid({url:"/library-mannager/book/manage/init.action"});
			});
		}
	});
});

var _addOrupdate = false;//true表示新增,false表示修改
function addInit(){
	$(".book").each(function(index){
		if(index != 4) {
			$(this).val("");
		}else{
			$(".book:eq(4)").datebox('setValue',"");
		}
	});
	$(".book").attr("readonly",false);
}
function updateInit(){
	$(".book:eq(0)").val($("#tab").datagrid("getSelected").bookId);
	$(".book:eq(1)").val($("#tab").datagrid("getSelected").bookName);
	$(".book:eq(2)").val($("#tab").datagrid("getSelected").bookAuthor);
	$(".book:eq(3)").val($("#tab").datagrid("getSelected").bookPublish);
	$(".book:eq(4)").datebox('setValue',$("#tab").datagrid("getSelected").bookDate);
	$(".book:eq(5)").val($("#tab").datagrid("getSelected").bookType);
	$(".book:eq(6)").val($("#tab").datagrid("getSelected").bookNumber);
	$(".book:eq(7)").val($("#tab").datagrid("getSelected").bookSummary);
	$(".book:eq(0)").attr("readonly",true);
}
function init() {
	$("#tab").datagrid({
		view: detailview,
		detailFormatter:function(index,row){
			return detail(row);
		} ,
		url:"/library-mannager/book/manage/init.action",
		singleSelect : true,
		fitColumns : true,
		pagination : true,
		fit : true,
		rownumbers:true,
		pagination:true  
		});
	var p = $('#tab').datagrid('getPager');  
    $(p).pagination({  
        beforePageText: '第',//页数文本框前显示的汉字  
        afterPageText: '页    共 {pages} 页',  
        displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录',  
    }); 
}
function detail(row){
	var _data = '<table width="100%"><tr><td width="12%" rowspan="5"><img src="' +row.bookImg + '" width="136" height="155" /></td>'
    +'<td width="44%">图书ID:'+row.bookId+'</td><td width="44%">图书名称:'+row.bookName+'</td></tr><tr><td>图书作者:'+row.bookAuthor
    +'</td><td>类别:'+row.bookType+'</td></tr><tr><td>出版商:'+row.bookPublish+'</td><td>出版日期:'+row.bookDate+'</td></tr>'
    +'<tr><td>负责人:'+row.managerName+'</td><td>在库数量:'+row.bookNumber+'</td></tr><tr><td colspan="2">摘要:'+row.bookSummary
    +'</td></tr><tr><td colspan="3">&nbsp;</td></tr></table>';
	_data += "<table width='100%'><tr><th>借阅者</th><th>书籍ID</th><th>数量</th><th>借书日期</th></tr>";
	$.ajax({
		async:false,
		type:"POST",
		url:"/library-mannager/brrowed/book/book.action",
		data:{bookId:row.bookId},
		success:function(data){
			for(var i = 0 ; i < data.data.length ; i++){
				_data =_data + "<tr><td>" + data.data[i].readerUsername + "</td><td>" + data.data[i].bookID + "</td><td>"
				+data.data[i].borrowedBookNumber+"</td><td>" + data.data[i].borrowedDate + "</td></tr>";
			}
		}
	});
	_data += "</table>";
	return _data;
}


//日期设置
function myformatter(date){
	var y = date.getFullYear();
	var m = date.getMonth()+1;
	var d = date.getDate();
	return y+'-'+(m<10?('0'+m):m)+'-'+(d<10?('0'+d):d);
}
function myparser(s){
	if (!s) return new Date();
	var ss = (s.split('-'));
	var y = parseInt(ss[0],10);
	var m = parseInt(ss[1],10);
	var d = parseInt(ss[2],10);
	if (!isNaN(y) && !isNaN(m) && !isNaN(d)){
		return new Date(y,m-1,d);
	} else {
		return new Date();
	}
}
//日期设置end